package com.foodev.maven.gwt;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.codehaus.plexus.util.FileUtils;

/**
 * Cleans up a GWT compilation.
 * 
 * @phase clean
 * @goal clean
 * @author Martin Algesten
 */
public class CleanMojo extends AbstractGwtMojo {

    public void execute() throws MojoExecutionException, MojoFailureException {

        try {

            deleteOutputDirs(modules);

        } catch (IOException ioe) {

            getLog().debug(ioe);

            throw new MojoFailureException("Failed to clean: " + ioe.getMessage());

        }

    }


    /**
     * Deletes the output directories.
     * 
     * @param modules
     *            the list of modules.
     * @throws IOException
     */
    private void deleteOutputDirs(List<String> modules) throws IOException {

        for (String module : modules) {

            File output = new File(webappSrc, module);

            if (output.exists()) {

                getLog().info("Deleting directory " + output.getAbsolutePath());

                FileUtils.deleteDirectory(output);

            }

            File outputAux = new File(webappSrc, module + "-aux");

            if (outputAux.exists()) {

                getLog().info("Deleting directory " + outputAux.getAbsolutePath());

                FileUtils.deleteDirectory(outputAux);

            }

        }

    }

}
